/**
 * 
 */
package com.cqrs.bey.ddd.impl;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

import com.cqrs.bey.ddd.DomainEvent;
import com.cqrs.bey.ddd.EventGate;

@Component
public class EventStandardGate implements EventGate {

	@Inject
	private EventRunEnvironment runEnvironment;

	@Override
	public Object dispatch(Object damainEvent) {

		if (isAsynchronous(damainEvent)) {
			// TODO add to the queue. Queue should send this command to the
			// RunEnvironment
			return null;
		}

		return runEnvironment.run(damainEvent);
	}

	/**
	 * @param command
	 * @return
	 */
	private boolean isAsynchronous(Object damainEvent) {
		if (!damainEvent.getClass().isAnnotationPresent(DomainEvent.class))
			return false;

		DomainEvent commandAnnotation = damainEvent.getClass().getAnnotation(
				DomainEvent.class);
		return commandAnnotation.asynchronous();
	}

}
